package com.kolon.business.mResourceRes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.DateUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonNews.controller.MResourceResViewDetailController")
public class MResourceResViewDetailController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mResourceRes/mResourceResViewDetail.do")
	public ModelAndView controller(HttpServletRequest request,
			
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String companyCD = (String)userMap.get("CompanyCD");
		String deptCode = (String)userMap.get("DeptCode");
		String type = request.getParameter("strResourceType");
		// 부서자원도 COMPANY로 등록해야함.
		if("DEPT".equals(type)) {
			type = "COMPANY";
		}
		String AMPM = ComUtil.getNullCheck(request.getParameter("AMPM"), "am"); //오전, 오후, 저녁 체크
		String YMD = ComUtil.getNullCheck(request.getParameter("YMD"), DateUtil.getToday());
		String year = request.getParameter("year") == null ? ComUtil.getNullCheck(YMD.substring(0, 4), DateUtil.getYearStr()) : request.getParameter("year");
		String month = request.getParameter("month") == null ? ComUtil.getNullCheck(YMD.substring(5, 7), DateUtil.getMonthStr()) : request.getParameter("month");
		String day = request.getParameter("day") == null ? ComUtil.getNullCheck(YMD.substring(8, 10), DateUtil.getDayStr()) : request.getParameter("day");
		if(month.length() <= 1) month = "0" + month;  
		if(day.length() <= 1) day = "0" + day;
		String  strSelDate = year + month + day;
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);			
		buffer.append("/resource_res.aspx?mode=").append("2");
		buffer.append("&strSelDate=").append(strSelDate);
		buffer.append("&strViewType=").append("0");
		buffer.append("&strResourceType=").append(type);
		buffer.append("&strCompanyCD=").append(companyCD);
		buffer.append("&strDeptCD=").append(deptCode);
		buffer.append("&strResourceID=").append(request.getParameter("strResourceID"));
		buffer.append("&userid=").append(id);
		buffer.append("&pw=").append(pw);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren();
		Iterator itXml = xmlList.iterator();
		Map map = null;
		List mapList = new ArrayList();
		String clickTime = ComUtil.getNullCheck(request.getParameter("clickTime"),"");
		String getStime = "";
		String getEtime = "";
		// XML 데이터 
		while (itXml.hasNext()) {
			Element el = (Element)itXml.next();
			map = new HashMap();
			map.put("ResourceID", el.getChild("ResourceID").getText());
			map.put("StartDate", el.getChild("StartDate").getText());
			map.put("RegUserID", el.getChild("RegUserID").getText());
			map.put("Resource_NAME", el.getChild("Resource_NAME").getText());
			map.put("START_TIME", el.getChild("START_TIME").getText().replaceAll(":", ""));
			map.put("START_TIME1", el.getChild("START_TIME").getText().replaceAll(":", "").substring(0, 2));
			map.put("START_TIME2", el.getChild("START_TIME").getText().replaceAll(":", "").substring(2, 4));
			map.put("END_TIME", el.getChild("END_TIME").getText().replaceAll(":", ""));
			map.put("END_TIME1", el.getChild("END_TIME").getText().replaceAll(":", "").substring(0, 2));
			map.put("END_TIME2", el.getChild("END_TIME").getText().replaceAll(":", "").substring(2, 4));
			map.put("RegUserID_INFO", el.getChild("RegUserID_INFO").getText());
			map.put("Contents", el.getChild("Contents").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("ResourceType", el.getChild("ResourceType").getText());
			map.put("Subject", el.getChild("Subject").getText());
			map.put("revflag", el.getChild("revflag").getText());
			if(ComUtil.iTos(Integer.parseInt(el.getChild("START_TIME").getText().replaceAll(":", ""))).length() == 3){
				getStime = ComUtil.toZeroStr(Integer.parseInt(el.getChild("START_TIME").getText().replaceAll(":", "")),4);
			}else{
				getStime = ComUtil.iTos(Integer.parseInt(el.getChild("START_TIME").getText().replaceAll(":", "")));						
			}
			if(ComUtil.iTos(Integer.parseInt(el.getChild("END_TIME").getText().replaceAll(":", ""))).length() == 3){
				getEtime = ComUtil.toZeroStr(Integer.parseInt(el.getChild("END_TIME").getText().replaceAll(":", "")),4);
			}else{
				getEtime = ComUtil.iTos(Integer.parseInt(el.getChild("END_TIME").getText().replaceAll(":", "")));						
			}
			if( (Integer.parseInt(clickTime) >= ComUtil.sToi(getStime) && Integer.parseInt(clickTime) < ComUtil.sToi(getEtime) ||
				 Integer.parseInt(clickTime.substring(0, 2)) == ComUtil.sToi(getStime.substring(0, 2)))
			) {
				mapList.add(map);
			}
		}	
		String step="";
		if("GROUP".equals(request.getParameter("strResourceType"))) {
			step = "그룹자원";
		}else if("COMPANY".equals(request.getParameter("strResourceType"))) {
			step = "회사자원";
		}else if("DEPT".equals(request.getParameter("strResourceType"))) {
			step = "부서자원";
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);			   //기본 정보 셋팅
		modelAndView.addObject("type", request.getParameter("strResourceType"));
		modelAndView.addObject("catid", request.getParameter("strResourceID"));
		modelAndView.addObject("ResourceNM", request.getParameter("ResourceNM"));
		modelAndView.addObject("ResType", request.getParameter("ResType"));
		modelAndView.addObject("ResTypeCatid", request.getParameter("ResTypeCatid"));
		modelAndView.addObject("ResTypeNM", ComUtil.getEncode(request.getParameter("ResourceNM"),"UTF-8"));
		modelAndView.addObject("step", step);
		modelAndView.addObject("YMD", YMD);
		modelAndView.addObject("year", year);
		modelAndView.addObject("month", month);
		modelAndView.addObject("day", day);
		modelAndView.addObject("AMPM", AMPM);
		return modelAndView;
	}	
	
}